package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Attendant;
import ex.com.jm.food.entity.Order;
import ex.com.jm.food.service.CompanyAttendantService;
import ex.com.jm.food.service.CompanyOrderService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;

/**
 * 订单管理action
 * @author wangshenfeng
 *
 */
public class CompanyOrderAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private CompanyOrderService companyOrderService;
	private CompanyAttendantService companyAttendantService;
	private Order o;
	private String comId;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String orderId;
	private String startDate;
	private String endDate;
	private String s_date;
	private String e_date;
	private Integer orderFlage;
	private String userName;
	private Integer payType;
	private Integer payStatus;
	private String order;
	private String sort;
	private List<Attendant> attendants=new ArrayList<Attendant>();
	private String street;
	private String attId;
	private Map<String, String> dateMap;
	public CompanyOrderService getCompanyOrderService() {
		return companyOrderService;
	}
	public void setCompanyOrderService(CompanyOrderService companyOrderService) {
		this.companyOrderService = companyOrderService;
	}
	public Order getO() {
		return o;
	}
	public void setO(Order o) {
		this.o = o;
	}
	public String getComId() {
		return comId;
	}
	public void setComId(String comId) {
		this.comId = comId;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	public String getStartDate() {
		return startDate;
	}
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}
	public String getEndDate() {
		return endDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public String getS_date() {
		return s_date;
	}
	public void setS_date(String s_date) {
		this.s_date = s_date;
	}
	public String getE_date() {
		return e_date;
	}
	public void setE_date(String e_date) {
		this.e_date = e_date;
	}
	public Integer getOrderFlage() {
		return orderFlage;
	}
	public void setOrderFlage(Integer orderFlage) {
		this.orderFlage = orderFlage;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public Integer getPayType() {
		return payType;
	}
	public void setPayType(Integer payType) {
		this.payType = payType;
	}
	public Integer getPayStatus() {
		return payStatus;
	}
	public void setPayStatus(Integer payStatus) {
		this.payStatus = payStatus;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public Map<String, String> getDateMap() {
		return dateMap;
	}
	public void setDateMap(Map<String, String> dateMap) {
		this.dateMap = dateMap;
	}
	public CompanyAttendantService getCompanyAttendantService() {
		return companyAttendantService;
	}
	public void setCompanyAttendantService(
			CompanyAttendantService companyAttendantService) {
		this.companyAttendantService = companyAttendantService;
	}
	public List<Attendant> getAttendants() {
		return attendants;
	}
	public void setAttendants(List<Attendant> attendants) {
		this.attendants = attendants;
	}
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public String getAttId() {
		return attId;
	}
	public void setAttId(String attId) {
		this.attId = attId;
	}
	/**
	 * 到达列表页面
	 * @return
	 */
	public String orderList(){
		dateMap=DateUtils.getDate();
		return "orderList";
	}
	
	/**
	 * 获取数据
	 */
	public void getOrders(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		int offset=(initPage-1)*pageRow;
		List<Order> orders=this.getCompanyOrderService().getOrders(offset, pageRow, comId, userName, payType, payStatus, orderFlage, s_date, e_date, order, sort);
		JSONArray joorder=JSONArray.fromObject(orders);
		totalCount=this.getCompanyOrderService().orderCount(comId, userName, payType, payStatus, orderFlage, s_date, e_date);
		JSONObject jo=JsonUtils.buildJson(joorder, initPage, pageRow, totalCount);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jo);
		out.print(jo);
		out.flush();
		out.close();
	}
	/**
	 * 获取某一个订单信息
	 * @return
	 */
	public String editOrder(){
		if(orderId!=null){
			o=this.getCompanyOrderService().editOrder(orderId);
		}
		return "edit";
	}
	/**
	 * 跟新
	 * @return
	 */
	public String updateOrder(){
		if(o.getUserName()==null||o.getUserName().equals("")){
			addFieldError("o.userName", "订单人姓名不能为空");
			return "edit";
		}
		if(o.getUserAddress()==null||o.getUserAddress().equals("")){
			addFieldError("o.userAddress", "订单人地址不能为空");
			return "edit";
		}
		if(o.getUserPhone()==null||o.getUserPhone().equals("")){
			addFieldError("o.userPhone", "订单人联系方式不能为空");
			return "edit";
		}
		this.getCompanyOrderService().updateOrder(o);
		return "list";
	}
	/**
	 * 更新订单状态
	 * @return
	 */
	public String updateFlage(){
		if(orderFlage==1){
			this.getCompanyOrderService().updateOrderStartDate(orderId, DateUtils.getOrderDate());
		}
		if(orderFlage==2){
			this.getCompanyOrderService().updateOrderEndDate(orderId, DateUtils.getOrderDate());
			this.getCompanyAttendantService().updateStatus(0, attId);
		}
		boolean flag=this.getCompanyOrderService().updateFlag(orderId, orderFlage);
		if(flag==true){
			return "list";
		}
		return "list";
	}
	/**
	 * 删除订单
	 * @return
	 */
	public String delOrder(){
		this.getCompanyOrderService().delOrder(orderId);
		return "list";
	}
	
	public String editAtten(){
		//o=this.getCompanyOrderService().editOrder(orderId);
		attendants=this.getCompanyAttendantService().getAttens(comId, street.split(";")[0]);
		return "editAtten";
		
	}
	
	public String updateAtten(){
		this.getCompanyOrderService().updateOrderStartDate(orderId, DateUtils.getOrderDate());
		this.getCompanyOrderService().updateFlag(orderId, 1);
		this.getCompanyAttendantService().updateStatus(1, attId);
		this.getCompanyOrderService().updateAtten(attId, orderId);
		return "list";
	}
	
}
